package com.xwt.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xwt.web.dao.UserRepository;
import com.xwt.web.entity.User;

@RestController
@RequestMapping("/auth")
public class AuthController {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private UserRepository userRepository;
	/**
	 * 注册用户
	 * @param user
	 * @return
	 */
	@RequestMapping("/register")
	public String register(@RequestBody User user) {
		logger.info(user.toString());
		if(user.getUsername()!=null) {
			User old=userRepository.findByUsername(user.getUsername());
			if(old!=null&&old.getUsername().equals(user.getUsername())) {
				return "用户名重复";
			}
			if(old==null) {
				String password=user.getPassword();
				BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
				user.setPassword(bCryptPasswordEncoder.encode(password.trim()));
				userRepository.save(user);
				return "注册成功";
			}
		}
		return null;
	}

}
